package net.dasviertel.buddylockscreen.activities;

import java.io.IOException;

import net.dasviertel.buddylockscreen.views.CreateImageView;

import android.app.Activity;
import android.app.WallpaperManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.monitorcontactschanges.R;

public class CreateImageActivity extends Activity {

	private CreateImageView createImageView;
	private Button saveButton, discardButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_create_image);

		saveButton = (Button) findViewById(R.id.bt_save_positions);
		discardButton = (Button) findViewById(R.id.bt_discard_save_positions);
		createImageView = (CreateImageView) findViewById(R.id.cv_generate_background_image);
		createImageView.init();
		createImageView.invalidate();

		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MediaStore.Images.Media.insertImage(getContentResolver(), createImageView.getBitmap(), "test" , "testDescription");
															
				WallpaperManager wallpaperManager =
				        WallpaperManager.getInstance(getApplicationContext());
				try {
					wallpaperManager.setBitmap(createImageView.getBitmap());
				} catch (IOException e) {

					e.printStackTrace();
				}
				
				finish();
			}
		});

		discardButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();

			}
		});

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onDestroy() {
		
		createImageView.recycleShownBitmap();
		super.onDestroy();
		
	}

	@Override
	public void onBackPressed() {

		createImageView.recycleShownBitmap();
		super.onBackPressed();

	}

}
